<?php
/*
 * @name 发起投票处理
 * @describe 登录的用户可以发起投票，包括两人的pk，多人的投票pk；发起投票时至少上传一张图片；
 * 上传的图片进行投票pk
 */
include 'checklogin.php';
if(!empty($_POST)){
    if(empty($_POST['votename'])){
        errorTip('vote.php',1,'请填写投票名称');exit;
    }

    if(empty($_POST['type'])){
        errorTip('vote.php',1,'请选择类型');exit;
    }
    if(empty($_POST['limitnum'])){
        errorTip('vote.php',1,'请选择限制次数');exit;
    }
    if(empty($_POST['starttime'])){
        errorTip('vote.php',1,'请填写投票开始时间');exit;
    }
    if(empty($_POST['endtime'])){
        errorTip('vote.php',1,'请填写投票结束时间');exit;
    }
    $data['name'] = $_POST['votename'];
    $data['type'] = $_POST['type'];
    $data['limit_times'] = $_POST['limitnum'];
    $data['is_del'] =0;
    $data['create_time'] = date("Y-m-d H:i:s");
    $data['start_time'] = $_POST['starttime'];
    $data['end_time'] = $_POST['endtime'];
    $data['create_id'] = $_SESSION['uid'];
    $data['create_name'] = $_SESSION['uname'];

    //连接数据库
    $link = mysql_connect('localhost', 'root', '');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }else{
        mysql_select_db('test', $link) or die('Could not select database.');
        mysql_query("set names 'utf8'",$link);
    }
    //添加发起的投票信息
    $result = mysql_query("insert into vote_profile(`name`,`create_id`,`create_name`,`limit_times`,`is_del`,`start_time`,`end_time`,`create_time`,`type`)values('{$data['name']}',{$data['create_id']},'{$data['create_name']}',{$data['limit_times']},0,'{$data['start_time']}','{$data['end_time']}','{$data['create_time']}',{$data['type']})", $link);
    $lastId = mysql_insert_id();
    if(empty($_FILES["imgval1"]['tmp_name']) && empty($_FILES["imgval2"]['tmp_name'])){
        errorTip('vote.php',1,'please upload pic');exit;
    }
    //上传图片处理
    if(!empty($_FILES["imgval1"]['tmp_name'])){
        if ((($_FILES["imgval1"]["type"] == "image/gif")
        || ($_FILES["imgval1"]["type"] == "image/jpeg")
        || ($_FILES["imgval1"]["type"] == "image/pjpeg") || $_FILES["imgval1"]["type"] == "image/x-png" || $_FILES["imgval1"]["type"] == "image/png")
        && ($_FILES["imgval1"]["size"] < 20000))
          {
          if ($_FILES["imgval1"]["error"] > 0)
            {
             $msg = "Return Code: " . $_FILES["imgval1"]["error"];echo $msg;exit;
             errorTip('vote.php',1,$msg);exit;
            }
          else
            {
                $arr = explode('.', $_FILES["imgval1"]["name"]);
                $str = $arr[1];
                $imgname = rand(9999999,5);
              move_uploaded_file($_FILES["imgval1"]["tmp_name"],
              "upload/" . $imgname.'.'.$str);
              $imgUrl = 'http://img.letian.com/'.$imgname.'.'.$str;
              $result = mysql_query("insert into vote_infos(`topic_id`,`create_id`,`img_url`,`vote_num`,`is_del`,`create_time`)values(".$lastId.",{$data['create_id']},'{$imgUrl}',0,0,'{$data['create_time']}')", $link);
            }
          }else{
            $msg = "请上传有效的图片";echo $msg;exit;
            errorTip('vote.php',1,$msg);exit;
          }
    }

    if(!empty($_FILES["imgval2"]['tmp_name'])){
        if ((($_FILES["imgval2"]["type"] == "image/gif")
        || ($_FILES["imgval2"]["type"] == "image/jpeg")
        || ($_FILES["imgval2"]["type"] == "image/pjpeg") || $_FILES["imgval1"]["type"] == "image/x-png" || $_FILES["imgval1"]["type"] == "image/png")
        && ($_FILES["imgval2"]["size"] < 20000))
          {
          if ($_FILES["imgval2"]["error"] > 0)
            {
                $msg = "Return Code: " . $_FILES["imgval2"]["error"];echo $msg;exit;
                errorTip('vote.php',1,$msg);exit;
            }
          else
            {
                $arr = explode('.', $_FILES["imgval2"]["name"]);
                $str = $arr[1];
                $imgname2 = rand(9999999,5);
                move_uploaded_file($_FILES["imgval2"]["tmp_name"],
              "upload/" . $imgname2.'.'.$str);
                $imgUrl2 = 'http://img.letian.com/'.$imgname2.'.'.$str;
              $result = mysql_query("insert into vote_infos(`topic_id`,`create_id`,`img_url`,`vote_num`,`is_del`,`create_time`)values(".$lastId.",{$data['create_id']},'{$imgUrl2}',0,0,'{$data['create_time']}')", $link);
            }
          }
        else
          {
            $msg = "请上传有效的图片";echo $msg;exit;
            errorTip('vote.php',1,$msg);exit;
          }
    }

    if(!$result){
        errorTip('vote.php',1);exit;
    }else{
        errorTip('upload.php',1,'发起投票成功，赶紧去上传图片吧');exit;
        // header("Location:vote.php");
    }

}else{
    include 'vote.html';
}

function errorTip($url,$time=1,$msg=''){
    $url = str_replace(array("\n", "\r"), '', $url);
    if (empty($msg))
        $msg = "系统将在{$time}秒之后自动跳转到{$url}！";
    if (!headers_sent()) {
        // redirect
        if (0 === $time) {
            header('Location: ' . $url);
        } else {
            header("refresh:{$time};url={$url}");
            echo($msg);
        }
        exit();
    } else {
        $str = "<meta http-equiv='Refresh' content='{$time};URL={$url};charset=utf-8' >";
        if ($time != 0)
            $str .= $msg;
        exit($str);
    }
}



?>